<!DOCTYPE html>
<HTML>
<HEAD>
	<TITLE> ZTREE DEMO - single path</TITLE>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<link rel="stylesheet" href="../../../css/demo.css" type="text/css">
	<link rel="stylesheet" href="../../../css/zTreeStyle/zTreeStyle.css" type="text/css">
	<script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script>
	<script type="text/javascript" src="../../../js/jquery.ztree.core-3.5.js"></script>
	<!--  <script type="text/javascript" src="../../../js/jquery.ztree.excheck-3.5.js"></script>
	  <script type="text/javascript" src="../../../js/jquery.ztree.exedit-3.5.js"></script>-->
	<SCRIPT type="text/javascript">
		<!--
		var setting = {
			view: {
				dblClickExpand: false,
				showLine: false
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				beforeExpand: beforeExpand,
				onExpand: onExpand,
				onClick: onClick
			}
		};

		var zNodes =[
			{ id:1, pId:0, name:"Root 1"},
			{ id:11, pId:1, name:"Parent Node 1-1"},
			{ id:111, pId:11, name:"Leaf Node 1-1-1"},
			{ id:1111, pId:111, name:"Leaf Node 1-1-1-1"},
			{ id:1112, pId:111, name:"Leaf Node 1-1-1-2"},
			{ id:1113, pId:111, name:"Leaf Node 1-1-1-3"},
			{ id:112, pId:11, name:"Leaf Node 1-1-2"},
			{ id:1121, pId:112, name:"Leaf Node 1-1-2-1"},
			{ id:1122, pId:112, name:"Leaf Node 1-1-2-2"},
			{ id:1123, pId:112, name:"Leaf Node 1-1-2-3"},
			{ id:113, pId:11, name:"Leaf Node 1-1-3"},
			{ id:114, pId:11, name:"Leaf Node 1-1-4"},
			{ id:12, pId:1, name:"Parent Node 1-2"},
			{ id:121, pId:12, name:"Leaf Node 1-2-1"},
			{ id:1211, pId:121, name:"Leaf Node 1-2-1-1"},
			{ id:1212, pId:121, name:"Leaf Node 1-2-1-2"},
			{ id:1213, pId:121, name:"Leaf Node 1-2-1-3"},
			{ id:122, pId:12, name:"Leaf Node 1-2-2"},
			{ id:1221, pId:122, name:"Leaf Node 1-2-2-1"},
			{ id:1222, pId:122, name:"Leaf Node 1-2-2-2"},
			{ id:1223, pId:122, name:"Leaf Node 1-2-2-3"},
			{ id:123, pId:12, name:"Leaf Node 1-2-3"},
			{ id:124, pId:12, name:"Leaf Node 1-2-4"},
			{ id:2, pId:0, name:"Root 2"},
			{ id:21, pId:2, name:"Parent Node 2-1"},
			{ id:211, pId:21, name:"Leaf Node 2-1-1"},
			{ id:212, pId:21, name:"Leaf Node 2-1-2"},
			{ id:213, pId:21, name:"Leaf Node 2-1-3"},
			{ id:214, pId:21, name:"Leaf Node 2-1-4"},
			{ id:22, pId:2, name:"Parent Node 2-2"},
			{ id:221, pId:22, name:"Leaf Node 2-2-1"},
			{ id:222, pId:22, name:"Leaf Node 2-2-2"},
			{ id:223, pId:22, name:"Leaf Node 2-2-3"},
			{ id:224, pId:22, name:"Leaf Node 2-2-4"}
		];

		var curExpandNode = null;
		function beforeExpand(treeId, treeNode) {
			var pNode = curExpandNode ? curExpandNode.getParentNode():null;
			var treeNodeP = treeNode.parentTId ? treeNode.getParentNode():null;
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			for(var i=0, l=!treeNodeP ? 0:treeNodeP.children.length; i<l; i++ ) {
				if (treeNode !== treeNodeP.children[i]) {
					zTree.expandNode(treeNodeP.children[i], false);
				}
			}
			while (pNode) {
				if (pNode === treeNode) {
					break;
				}
				pNode = pNode.getParentNode();
			}
			if (!pNode) {
				singlePath(treeNode);
			}

		}
		function singlePath(newNode) {
			if (newNode === curExpandNode) return;
			if (curExpandNode && curExpandNode.open==true) {
				var zTree = $.fn.zTree.getZTreeObj("treeDemo");
				if (newNode.parentTId === curExpandNode.parentTId) {
					zTree.expandNode(curExpandNode, false);
				} else {
					var newParents = [];
					while (newNode) {
						newNode = newNode.getParentNode();
						if (newNode === curExpandNode) {
							newParents = null;
							break;
						} else if (newNode) {
							newParents.push(newNode);
						}
					}
					if (newParents!=null) {
						var oldNode = curExpandNode;
						var oldParents = [];
						while (oldNode) {
							oldNode = oldNode.getParentNode();
							if (oldNode) {
								oldParents.push(oldNode);
							}
						}
						if (newParents.length>0) {
							for (var i = Math.min(newParents.length, oldParents.length)-1; i>=0; i--) {
								if (newParents[i] !== oldParents[i]) {
									zTree.expandNode(oldParents[i], false);
									break;
								}
							}
						} else {
							zTree.expandNode(oldParents[oldParents.length-1], false);
						}
					}
				}
			}
			curExpandNode = newNode;
		}

		function onExpand(event, treeId, treeNode) {
			curExpandNode = treeNode;
		}

		function onClick(e,treeId, treeNode) {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			zTree.expandNode(treeNode, null, null, null, true);
		}

		$(document).ready(function(){
			$.fn.zTree.init($("#treeDemo"), setting, zNodes);
		});
		//-->
	</SCRIPT>
	<style type="text/css">
.ztree li button.switch {visibility:hidden; width:1px;}
.ztree li button.switch.roots_docu {visibility:visible; width:16px;}
.ztree li button.switch.center_docu {visibility:visible; width:16px;}
.ztree li button.switch.bottom_docu {visibility:visible; width:16px;}
	</style>
 </HEAD>

<BODY>
<h1>Keep Single Path</h1>
<h6>[ File Path: super/singlepath.html ]</h6>
<div class="content_wrap">
	<div class="zTreeDemoBackground left">
		<ul id="treeDemo" class="ztree"></ul>
	</div>
	<div class="right">
		<ul class="info">
			<li class="title"><h2>Explanation of implementation method</h2>
				<ul class="list">
				<li>This Demo is the transformation from "Click to Expand Node" demo​​, tree only expand single path.</li>
				<li class="highlight_red">Use 'setting.callback.beforeExpand / onExpand' callback function to achieve rules about expand </li>
				</ul>
			</li>
		</ul>
	</div>
</div>
</BODY>
</HTML>